//
//  AppDelegate.m
//  PersonnelWashCar
//
//  Created by 张越 on 16/4/20.
//  Copyright © 2016年 张越. All rights reserved.
//

#import "AppDelegate.h"
#import "HomePageController.h"
#import "OrderViewController.h"
#import "MineViewController.h"
#import "LoginViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

@interface AppDelegate ()
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) BaseNavController *baseNav;
@property (nonatomic, strong) UITabBarController *tabView;

@end

@implementation AppDelegate
@synthesize window;

- (CLLocationManager *) locationManager {
    if (_locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc]init];
    }
    return _locationManager;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor colorWithWhite:0.933 alpha:1.000];
    [self.window makeKeyAndVisible];
    HomePageController *homePageVC = [[HomePageController alloc]init];
    homePageVC.title = @"抢单";
    homePageVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:homePageVC.title image:[UIImage imageNamed:@"抢单"] selectedImage:[UIImage imageNamed:@"抢单选中"]];
    BaseNavController *homePageNav = [[BaseNavController alloc] initWithRootViewController:homePageVC];
    
    OrderViewController *orderVC = [[OrderViewController alloc]init];
    orderVC.title = @"我的订单";
    orderVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:orderVC.title image:[UIImage imageNamed:@"我的订单"] selectedImage:[UIImage imageNamed:@"我的订单选中"]];
    BaseNavController *orderNav = [[BaseNavController alloc] initWithRootViewController:orderVC];
    
    MineViewController *mineVC = [[MineViewController alloc]init];
    mineVC.title = @"我的";
    mineVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:mineVC.title image:[UIImage imageNamed:@"我的"] selectedImage:[UIImage imageNamed:@"我的选中"]];
    BaseNavController *mineNav = [[BaseNavController alloc] initWithRootViewController:mineVC];
    
    // tab bar
    UITabBarController *rootTabBarController = [[UITabBarController alloc] init];
    rootTabBarController.viewControllers = @[homePageNav,orderNav,mineNav];
    self.tabView = rootTabBarController;
    self.baseNav = homePageNav;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(seleCtRootView:) name:@"SelectTabRootView" object:nil];
    NSString *userid = [[NSUserDefaults standardUserDefaults]objectForKey:KUserDefaultsUserIdKey];
    if(userid == nil)
    {
        
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        BaseNavController *loginNav = [[BaseNavController alloc] initWithRootViewController:loginVC];
        self.window.rootViewController = loginNav;
    }else{
        self.window.rootViewController = rootTabBarController;
    }
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SelectWindowView:) name:@"SelectWindowView" object:nil];

#pragma mark ---配置百度地图---
    // 要使用百度地图，请先启动BaiduMapManager
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
    BOOL ret = [_mapManager start:@"YOCSdImWpXwtos1uVZ9hejYEfGvWzqO3"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    // Add the navigation controller's view to the window and display.
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    
    //如果没有授权则请求用户授权
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestWhenInUseAuthorization];
    } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
    }
    
    return YES;
}

//-(void)SelectWindowView:(NSNotification *)no
//{
//    self.window.rootViewController = self.tabView;
//    
//}

-(void)seleCtRootView:(NSNotification *)noti
{
    self.tabView.selectedViewController = self.baseNav;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
